﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace demo
{
    class Program
    {
        static async void Main(string[] args)
        {
            var tokenSource2 = new CancellationTokenSource();
            CancellationToken ct = tokenSource2.Token;
            var tokenSource3 = new CancellationTokenSource();

            var task = Task.Run(() =>
            {
                // Were we already canceled?
                ct.ThrowIfCancellationRequested();

                bool moreToDo = true;
                while (moreToDo)
                {
                    // Poll on this property if you have to do
                    // other cleanup before throwing.
                    // Clean up here, then...
                    if (ct.IsCancellationRequested)
                    {
                        ct.ThrowIfCancellationRequested();
                    }
                }
            }, tokenSource2.Token); // Pass same token to Task.Run.

            Thread.Sleep(5000);
            tokenSource2.Cancel();

            // Just continue on this thread, or await with try-catch:
            try
            {
                await task;
            }
            catch (OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
                bool eqs = e.CancellationToken.Equals(tokenSource3.Token);
                System.Console.WriteLine($"Equals' result: {eqs}.");
                System.Console.WriteLine($"Task's status: {task.Status}");
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            finally
            {
                tokenSource2.Dispose();
            }

            Console.ReadKey();
            //GregorianCalendar gc = new GregorianCalendar();
            //int a = gc.GetWeekOfYear(DateTime.Parse("2018.12.31"),2CalendarWeekRule.FirstDay, DayOfWeek.Monday);

            #region MyRegion

            ////测试异步
            //AsyncWait asyncWait = new AsyncWait();
            //asyncWait.Start();

            //浅复制&深复制
            //DeepCopy deepCopy = new DeepCopy();
            //deepCopy.Start();

            //using (SecureString ss = new SecureString())
            //{
            //    // 只能逐字符地操作SecureString对象
            //    ss.AppendChar('e');
            //    ss.AppendChar('i');
            //    ss.AppendChar('s');
            //    ss.AppendChar('o');
            //    ss.AppendChar('n');
            //    ss.InsertAt(1, 'd');
            //    // 打印SecureStrign对象
            //    PrintSecureString(ss);
            //}

            //int[] array = new[] { 9, 8, 7, 6, 5, 4, 3, 2 };

            //SortHelper<int>.BubbleSort(array);

            //foreach (var item in array)
            //{
            //    Console.WriteLine(item);
            //}

            //GZipTools.Zip();

            //使用日志
            //using (LogManager logmanager = new LogManager(Type.GetType("demo.Program"), "E:\\TestLog.txt"))
            //{
            //    logmanager.WriteLog("新建了日志", LogType.Debug);
            //    logmanager.WriteLog("写数据", LogType.Debug);
            //    logmanager.UseUTCTime();
            //    logmanager.WriteLog("现在是UTC时间", LogType.Debug);
            //    logmanager.UseLocalTime();
            //    logmanager.WriteLog("回到本地时间", LogType.Debug);
            //    logmanager.WriteLog("发生错误", LogType.Error);
            //    logmanager.WriteLog("准备退出", LogType.Info);
            //}


            //// 控制台事件管理者
            //ConsoleManager cm = new ConsoleManager();
            //// 控制台事件订阅者
            //EventTest.Log log = new EventTest.Log(cm);

            //cm.ConsoleOutput("测试控制台输出事件");
            //cm.ConsoleOutput("测试控制台输出事件");
            //cm.ConsoleOutput("测试控制台输出事件");


            //string sDate = "5/2/2005"; //本意是2005年5月2号
            //DateTime[] dt = new DateTime[3];
            //CultureInfo[] cf = new CultureInfo[3];
            //cf[0] = new CultureInfo("en-US", true); //指定为en-US,字符串将被解释为“MM/DD/YYYY”
            //dt[0] = DateTime.Parse(sDate, cf[0], DateTimeStyles.AllowWhiteSpaces);
            //dt[0] = dt[0].AddMonths(1); //另日期变为2005年6月2日

            //Console.WriteLine(dt[0].ToString(cf[0]));

            //cf[1] = new CultureInfo("hi-IN", true); //这是印度格式，字符串被解释为“DD/MM/YYYY”
            //dt[1] = DateTime.Parse(sDate, cf[1], DateTimeStyles.AllowWhiteSpaces);
            //dt[1] = dt[1].AddMonths(1); //让它变成2005年3月5日
            //Console.WriteLine(dt[1].ToString(cf[1]));


            //int maxSize = 100;
            //int[] array = new int[maxSize];
            //Random random = new Random();
            //for (int i = 0; i < maxSize; i++)
            //{
            //    array[i] = random.Next(1, maxSize + 1);
            //}

            //Stopwatch watcher = new Stopwatch();
            //watcher.Start();
            //SortHelper<int>.BubbleSort(array);
            //watcher.Stop();

            //Console.WriteLine("Total Elapsed Milliseconds:{0}ms", watcher.ElapsedMilliseconds);
            //watcher = new Stopwatch();
            //watcher.Start();
            //SortHelper<int>.QuickSort(array, 0, array.Length - 1);
            //watcher.Stop();
            //Console.WriteLine("Total Elapsed Milliseconds:{0}ms", watcher.ElapsedMilliseconds);

            //foreach (var item in array)
            //{
            //    Console.WriteLine(item);
            //}

            //string[] phones = { "029-88401100", "029-88500321" };
            //Address a = new Address("陕西", "西安", "710068", phones);
            //Console.WriteLine(a.Phones[0]); // 输出: 029-88401100
            //phones[0] = "029-XXXXXXXX"; // 通过phones变量修改了Address对象内部的数据
            //Console.WriteLine(a.Phones[0]); // 输出: 029-XXXXXXXX}
            #endregion
        }
    }
}